package cn.codergege.shiro.chapter6.service.impl;

import java.util.Set;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import cn.codergege.shiro.chapter6.dao.UserDao;
import cn.codergege.shiro.chapter6.model.User;
import cn.codergege.shiro.chapter6.service.UserService;
import cn.codergege.shiro.chapter6.util.PasswordHelper;

@Service("userService")
public class UserServiceImpl implements UserService {
	
	@Resource
	private UserDao userDao;
	@Resource
	private PasswordHelper passwordHelper;

	@Override
	public User createUser(User user) throws Exception {
		// 加密密码
		passwordHelper.encryptPassword(user);
		return userDao.createUser(user);
	}

	@Override
	public void changePassword(Long userId, String newPassword) throws Exception {
		User user = userDao.findOne(userId);
		user.setPassword(newPassword);
		passwordHelper.encryptPassword(user);
		userDao.updateUser(user);
	}

	@Override
	public void correlationRoles(Long userId, Long... roleIds) throws Exception {
		userDao.correlationRoles(userId, roleIds);
	}

	@Override
	public void uncorrelationRoles(Long userId, Long... roleIds) throws Exception {
		userDao.uncorrelationRoles(userId, roleIds);
	}

	@Override
	public User findByUsername(String username) {
		System.out.println(userDao);
		return userDao.findByUsername(username);
	}

	@Override
	public Set<String> findRoles(String username) {
		return userDao.findRoles(username);
	}

	@Override
	public Set<String> findPermissions(String username) {
		return userDao.findPermissions(username);
	}
}
